﻿namespace com.oakwoodsys.ConsultantCare
{
    using System;

    using Microsoft.SharePoint;

    /// <summary>
    /// List Item Events
    /// </summary>
    public class DeletionItemEventReceiver : SPItemEventReceiver
    {
        #region Methods

        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded( SPItemEventProperties properties )
        {
            SPListItem item = properties.ListItem;
            string s = item.Title;
            string[ ] a = s.Split( ':' );
            if ( a.Length >= 2 )
            {
                string action = a[ 0 ];
                Guid id = new Guid( a[ 1 ] );
                if ( action.Equals( "A", StringComparison.OrdinalIgnoreCase ) )
                {
                    deleteByActivity( properties, id );
                }
                else if ( action.Equals( "C", StringComparison.OrdinalIgnoreCase ) )
                {
                    deleteByConsultant( properties, id );
                }
            }

            item.Delete( );
        }

        void deleteByActivity( SPItemEventProperties properties, Guid id )
        {
            CalendarEventCreator creator = new CalendarEventCreator( properties.WebUrl );
            creator.RemoveCalendarEventsByActivity( id );
        }
        void deleteByConsultant( SPItemEventProperties properties, Guid id )
        {
            CalendarEventCreator creator = new CalendarEventCreator( properties.WebUrl );
            creator.RemoveCalendarEventsByConsultant( id );
        }

        #endregion Methods
    }
}